package cn.xz.zysjz.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.WindowManager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Objects;

import cn.xz.zysjz.R;
import cn.xz.zysjz.base.BaseActivity;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.UserBean;
import cn.xz.zysjz.databinding.ActivityEditBinding;
import cn.xz.zysjz.dialog.ShowDialog;
import cn.xz.zysjz.utils.AppHelper;
import cn.xz.zysjz.utils.SessionUtils;
import cn.xz.zysjz.utils.ToastUtils;

/**
 * 编辑信息界面*/
public class EditActivity extends BaseActivity {

    ActivityEditBinding mBinding;

    private int mTK = 10010; //相册返回码

    private String mUrl = ""; //头像地址

    private String mPwd = ""; //密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_edit);
        if (Build.VERSION.SDK_INT >= 21) {
            this.getWindow()
                    .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        initEvent();
        initData();
    }

    private void initData(){
        //获取用户信息
        //用户名
        if (CommonLib.nowUserBean != null){
            //头像
            if (!Objects.equals(CommonLib.nowUserBean.getPic(), "")){
                AppHelper.setPic(mBinding.ivHead,CommonLib.nowUserBean.getPic());
                mUrl = CommonLib.nowUserBean.getPic();
            }
            //密码
            mPwd = CommonLib.nowUserBean.getPwd();
            mBinding.tvMm.setText("密码: " + CommonLib.nowUserBean.getPwd());
            //用户名
            mBinding.tvName.setText(CommonLib.nowUserBean.getName().toString());
            //性别
            if (Objects.equals(CommonLib.nowUserBean.getSex(), "1")){
                //男
                mBinding.rb1.setChecked(true);
                mBinding.rb0.setChecked(false);
            }else {
                mBinding.rb1.setChecked(false);
                mBinding.rb0.setChecked(true);
            }
            //年龄
            mBinding.tvOld.setText(CommonLib.nowUserBean.getOld());
        }
    }

    private void initEvent(){
        //返回
        mBinding.viewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //点击头像
        mBinding.cvHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开相册
                Intent intent  = new Intent(Intent.ACTION_PICK);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, mTK);
            }
        });
        //点击昵称
        mBinding.tvName.setOnClickListener(v->{
            ShowDialog.show2("请输入昵称", this, new ShowDialog.OnShowClickStr() {
                @Override
                public void click(String s) {
                    mBinding.tvName.setText(s);
                }
            });
        });
        //点击年龄
        mBinding.tvOld.setOnClickListener(v->{
            ShowDialog.show2("请输入年龄", this, new ShowDialog.OnShowClickStr() {
                @Override
                public void click(String s) {
                    mBinding.tvOld.setText(s);
                }
            });
        });
        //点击修改
        mBinding.tvYy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<UserBean> list = SessionUtils.getList(CommonLib.USER_DATA, UserBean.class);
                for (UserBean userBean1 : list){
                    if (Objects.equals(userBean1.getName(), CommonLib.nowUserBean.getName())){
                        userBean1.setPic(mUrl);
                        userBean1.setName(mBinding.tvName.getText().toString());
                        userBean1.setOld(mBinding.tvOld.getText().toString());
                        String mS = "1";
                        if (mBinding.rb0.isChecked()) {
                            mS = "0";
                        }
                        userBean1.setPwd(mPwd);
                        userBean1.setSex(mS);
                        CommonLib.nowUserBean = userBean1;
                    }
                }
                SessionUtils.commitList(CommonLib.USER_DATA, list);
                ToastUtils.show("修改成功");
            }
        });
        //点击密码
        mBinding.tvMm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowDialog.show2("请输入密码", EditActivity.this, new ShowDialog.OnShowClickStr() {
                    @Override
                    public void click(String s) {
                        mPwd = s;
                        mBinding.tvMm.setText("密码: " + s);
                    }
                });
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == mTK){
            if (data != null){
                mUrl = data.getData().toString();
                AppHelper.setPic(mBinding.ivHead,mUrl);
            }
        }
    }
}